var express = require('express');
var router = express.Router();
var User = require('../models/User');
var Content = require('../models/Content');

// 统一返回格式
var responseData;

router.use( function (req, res, next) {
   responseData = {
       code : 0,
       message : ''
   } ;
   next();
});

// 登录接口
router.post('/user/login', function (req, res) {
    var username = req.body.username;
    var password = req.body.password;

    // 表单验证
    if(!checkForm(username, password)){
        res.json(responseData);
        return;
    }
    
    // 查找用户
    User.findOne({
        username: username,
        password: password
    }).then(function (userInfo) {
        if(!userInfo){
            responseData.code = 1;
            responseData.message = '用户名或密码错误';
        }else{
            responseData.code = 0;
            responseData.userInfo = {
                uid : userInfo._id,
                username : userInfo.username
            };
            responseData.message = '登录成功';
            console.log('cookies',req.cookies);
            console.log('JSON',JSON.stringify({
                uid : userInfo._id,
                username : userInfo.username
            }));
            req.cookies.set('Content-Length', Buffer.byteLength(data, 'utf-8'));
            req.cookies.set('userInfo', JSON.stringify({
                uid : userInfo._id,
                username : userInfo.username
            }));
        }
        console.log(1);
        res.json(responseData);
    })

});

// 注册接口
router.post('/user/register', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    var repassword = req.body.repassword;

    if(!checkForm(username, password, repassword)){
        res.json(responseData);
        return;
    }

    //用户是否已经注册
    User.findOne({
        username: username
    }).then(function (userInfo) {
        if (userInfo){
            // 表示数据库中有该记录
            responseData.code = 4;
            responseData.message = '用户名已经被注册';
            res.json(responseData);
            return;
        }
        // 保存用户
        var user = new User({
            username: username,
            password: password
        });
        return user.save();
    }).then(function (newUserInfo) {
        // console.log('newUserInfo',newUserInfo)
        responseData.code = 0;
        responseData.message = '注册成功';
        res.json(responseData);
        return;
    });

});

// 登录注册表单验证
function checkForm( username, password , repassword) {

    // 用户是否为空
    if( username == ''){
        responseData.code = 1;
        responseData.message = '用户名不能为空';
        return false;
    }
    // 密码是否为空
    if( password == ''){
        responseData.code = 1;
        responseData.message = '密码不能为空';
        return false;
    }
    // 两次输入的密码是否一致
    if (repassword && password != repassword){
        responseData.code = 1;
        responseData.message = '两次输入的密码不一致';
        return false;
    }

    return true;
}

// 登出接口
router.get('/user/logout', function (req, res) {
   req.cookies.set('userInfo', null);
   responseData.code = 0;
   responseData.message = '退出成功!';
   res.json(responseData);
});

/*
* 评论提交
* */
router.post('/comment/post', function (req, res) {
    // 内容的id
    var contentId = req.body.contentid || '';
    var postData = {
        username: req.userInfo.username,
        postTime: new Date(),
        content: req.body.commentContent
    }

    // 查询当前文章内容的信息
    Content.findOne({ _id: contentId }).then(function (content) {
        content.comments.push(postData);
        return content.save();
    }).then(function (newContent) {
        responseData.message = '评论成功';
        responseData.data = newContent;
        res.json(responseData);
    })
});

/*
* 获取指定文章的所有评论
* */
router.get('/comment', function (req, res) {
    var contentId = req.query.contentid || '';

    Content.findOne({ _id: contentId }).then(function (content) {
        responseData.data = content.comments;
        res.json(responseData);
    })
});

module.exports = router;